import {Component, OnInit} from '@angular/core';
import {OperateService} from '../operate.service';
import {NzMessageService} from 'ng-zorro-antd/message';
import {DatePipe} from '@angular/common';
import {Router} from '@angular/router';

enum TYPE {
  '实物' = 1,
  '虚拟-积分',
  '虚拟-礼包兑换码',
}

@Component({
  selector: 'app-mall',
  templateUrl: './mall.component.html',
  styleUrls: ['./mall.component.scss']
})
export class MallComponent implements OnInit {
  static updateCache: () => void;
  query = {
    goods: '',
    state: '',
    time: [],
    type: 1,
    page: 1,
    pageSize: 10
  };
  tableData = {
    records: [],
    total: 0,
    checked: false,
    indeterminate: false,
    setOfCheckedId: new Set<number>(),
  };
  TYPE = TYPE;

  constructor(
    private operateService: OperateService,
    private datePipe: DatePipe,
    private nzMessageService: NzMessageService,
    private router: Router,
  ) {
  }

  ngOnInit(): void {
    this.getGoods();
    MallComponent.updateCache = () => {
      this.getGoods();
    };
  }

  // 实物1 虚拟2
  tabClick(type: 1 | 2) {
    this.query = {
      goods: '',
      state: '',
      time: [],
      type,
      page: 1,
      pageSize: 10
    };
    this.getGoods();
  }

  getGoods() {
    this.operateService.getGoodsList({
      goods: this.query.goods,
      state: this.query.state,
      beginTime: this.datePipe.transform(this.query.time[0], 'yyyy-MM-dd'),
      endTime: this.datePipe.transform(this.query.time[1], 'yyyy-MM-dd'),
      type: this.query.type,
      page: this.query.page,
      pageSize: this.query.pageSize,
    }).then((res: any) => {
      if (res.code === 0) {
        this.tableData.records = res.data.records;
        this.tableData.total = res.data.total;
      }
    });
  }

  onPageIndexChange(index: number) {
    this.query.page = index;
    this.getGoods();
  }

  onPageSizeChange(pageSize: number) {
    this.query.pageSize = pageSize;
    this.getGoods();
  }


  onAllChecked(checked: boolean): void {
    this.tableData.records
      // // @ts-ignore
      // .filter(({disabled, status}) => !disabled && status !== 4)
      // @ts-ignore
      .forEach(({id}) => this.updateCheckedSet(id, checked));
    this.refreshCheckedStatus();
  }

  refreshCheckedStatus(): void {
    // @ts-ignore
    const listOfEnabledData = this.tableData.records;
    // @ts-ignore
    this.tableData.checked = listOfEnabledData.every(({id}) => this.tableData.setOfCheckedId.has(id));
    // @ts-ignore
    this.tableData.indeterminate = listOfEnabledData.some(({id}) => this.tableData.setOfCheckedId.has(id)) && !this.tableData.checked;
  }

  onItemChecked(id: number, checked: boolean): void {
    this.updateCheckedSet(id, checked);
    this.refreshCheckedStatus();
  }

  updateCheckedSet(id: number, checked: boolean): void {
    if (checked) {
      this.tableData.setOfCheckedId.add(id);
    } else {
      this.tableData.setOfCheckedId.delete(id);
    }
  }

  queryBtn() {
    this.query.page = 1;
    this.tableData.checked = false;
    this.tableData.indeterminate = false;
    this.tableData.setOfCheckedId = new Set<number>();
    this.refreshCheckedStatus();
    this.getGoods();
  }

  resetCondition() {
    this.query.goods = '';
    this.query.state = '';
    this.query.time = [];
    this.query.page = 1;
    this.query.pageSize = 10;
    this.getGoods();
  }

  changeGoodsStatusAll(type: 0 | 1) {
    if (!this.tableData.setOfCheckedId.size) {
      this.nzMessageService.warning('请选择要操作的数据');
      return;
    }
    this.operateService.changeGoodsStatus(Array.from(this.tableData.setOfCheckedId), type).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.tableData.checked = false;
        this.tableData.indeterminate = false;
        this.tableData.setOfCheckedId = new Set<number>();
        this.refreshCheckedStatus();
        this.getGoods();
      }
    });
  }

  changeGoodsStatus(id: any, type: 0 | 1) {
    this.operateService.changeGoodsStatus([id], type).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getGoods();
      }
    });
  }

  deleteGoods(id: any) {
    this.operateService.addAndModifyGoods({
      id,
      status: 0
    }).then((res: any) => {
      if (res.code === 0) {
        this.nzMessageService.success('操作成功');
        this.getGoods();
      }
    });
  }

  modifyGoods(data: any) {
    this.router.navigate(['/promotionshare/operate-editGoods'], {queryParams: {goodsDetail: JSON.stringify(data)}});
  }
}
